package anatex.domain;

import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import javax.persistence.OrderBy;
import org.springframework.format.annotation.DateTimeFormat;
import anatex.domain.TaskState.EnumState;
import anatex.domain.Document;
import javax.persistence.OneToOne;

@RooJavaBean
@RooToString
@RooEntity(finders = { "findTasksByCurrentStateAndEnabled", "findTasksByDocument" })
public class Task {

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "SS")
    private Date startTime;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "SS")
    private Date finishTime;

    private Boolean enabled;

    public enum EnumState {

        NOT_STARTED, WORKING, FINISHED
    }

    ;

    private EnumState currentState;

    @OneToOne
    private Document document;

    public static TypedQuery<Task> findTasksByCurrentStateAndEnabled(EnumState currentState, Boolean enabled) {
        if (currentState == null) throw new IllegalArgumentException("The currentState argument is required");
        if (enabled == null) throw new IllegalArgumentException("The enabled argument is required");
        EntityManager em = Task.entityManager();
        TypedQuery<Task> q = em.createQuery("SELECT Task FROM Task AS task WHERE task.currentState = :currentState AND task.enabled = :enabled ", Task.class);
        q.setParameter("currentState", currentState);
        q.setParameter("enabled", enabled);
        return q;
    }

    public static Task findRecommendedTask(EnumState currentState, Boolean enabled) {
        if (currentState == null) throw new IllegalArgumentException("The currentState argument is required");
        if (enabled == null) throw new IllegalArgumentException("The enabled argument is required");
        EntityManager em = Task.entityManager();
        TypedQuery<Task> q = em.createQuery("SELECT Task FROM Task AS task WHERE task.currentState = :currentState AND task.enabled = :enabled ", Task.class);
        q.setParameter("currentState", currentState);
        q.setParameter("enabled", enabled);
        q.setMaxResults(1);
        try{
        	System.out.println("findRecommendedTask error:");
        }catch(Exception ex){
        	ex.printStackTrace(System.out);
        }
        return q.getSingleResult();
    }
    
    public static Task findTaskByDocument(Document document) {
        if (document == null) throw new IllegalArgumentException("The document argument is required");
        EntityManager em = Task.entityManager();
        TypedQuery<Task> q = em.createQuery("SELECT Task FROM Task AS task WHERE task.document = :document", Task.class);
        q.setParameter("document", document);
        q.setMaxResults(1);
        return q.getSingleResult();
    }
    
    public void setCurrentState (EnumState currentState) {
    	this.currentState = currentState;
    }
}
